using System;
using JetBrains.Annotations;
using Kaleida.ServiceMonitor.Framework;
using Kaleida.ServiceMonitor.Model.Runtime;

namespace Kaleida.ServiceMonitor.Model.ScriptDirectives
{
    public class PollFrequencyDirective : IScriptDirective
    {
        private readonly Duration pollFrequency;

        public PollFrequencyDirective([NotNull] string pollFrequency)
        {
            if (pollFrequency == null) throw new ArgumentNullException("pollFrequency");

            var frequency = pollFrequency.ToDuration();

            if (!PollFrequencyLimit.IsValid(frequency))
                throw new InvalidOperationException(string.Format("{0} is not a valid poll frequency. {1}", pollFrequency, PollFrequencyLimit.GetSummary()));

            this.pollFrequency = frequency;
        }

        public void Apply([NotNull] CompiledScript script)
        {
            if (script == null) throw new ArgumentNullException("script");

            script.PollFrequency = pollFrequency;
        }
    }
}